package com.iemes.controller.metals;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.AuditLoggingFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.MetalsBomFormMap;
import com.iemes.entity.MetalsReceiveFormMap;
import com.iemes.entity.PositionFormMap;
import com.iemes.entity.PurchaseRequestFormMap;
import com.iemes.entity.PurchaseSesaleStockFromMap;
import com.iemes.entity.ReceiveFormMap;
import com.iemes.entity.RepairapplicationFromMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.DepartmentMapper;
import com.iemes.mapper.PositionMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.mapper.metals.AuditLoggingMapper;
import com.iemes.mapper.metals.MetalsBomMapper;
import com.iemes.mapper.metals.MetalsReceiveMapper;
import com.iemes.mapper.metals.RepairapplicationMapper;
import com.iemes.mapper.warehouse.PurchasesalestockMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;
@RequestMapping("/receive")
@Controller
public class ReceiveFormMapController extends BaseController {
	
	@Inject
	private MetalsReceiveMapper receiveMapper;
	@Inject
	private UserMapper userMapper;
	@Inject
	private MetalsBomMapper metalsBomMapper;
	@Inject 
	private DepartmentMapper departmentMapper;
	@Inject
	private RepairapplicationMapper repairapplicationMapper;
	@Inject
	private AuditLoggingMapper auditLoggingMapper;
	@Inject
	private PurchasesalestockMapper purchasesalestockMapper;
	@Inject
	private PositionMapper positionMapper;
	
	@RequestMapping("receive-count")
	public String receiveCount(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/metals/receive/receive_statistics_list";
	}
	
	@RequestMapping("receive")
	public String receive(Model model){
		List<DepartmentForMap> departmentForMap=departmentMapper.findByWhere(new DepartmentForMap());
		MetalsBomFormMap metalsBomFormMap = new MetalsBomFormMap();
		metalsBomFormMap.put("where", "where state=0");
		List<MetalsBomFormMap> metalsBomFormMaps = metalsBomMapper.findByWhere(metalsBomFormMap);
		
		model.addAttribute("department", departmentForMap);
		model.addAttribute("metalsbom", metalsBomFormMaps);
		return Common.BACKGROUND_PATH + "/metals/receive/receive_statistics_add";
	}
	
	@ResponseBody
	@RequestMapping(value="metalsbom_choose")
	public MetalsBomFormMap emetalsbom_choose(String id) throws Exception {
		//获取物料对象		
		MetalsBomFormMap metalsBomFormMap = metalsBomMapper.findbyFrist("id", id, MetalsBomFormMap.class);
			if(metalsBomFormMap.get("oldForNewService").toString().equals("0")) {
				metalsBomFormMap.set("oldForNewService", "否");
			}else {
				metalsBomFormMap.set("oldForNewService", "是");
			}
		return metalsBomFormMap;
	}
	
	@ResponseBody
	@RequestMapping("receive_findByPage")
	public PageView findByPage(String pageNow, String pageSize) 
	{
		MetalsReceiveFormMap receiveFormMap = getFormMap(MetalsReceiveFormMap.class);
		MetalsBomFormMap metalsBomFormMap = getFormMap(MetalsBomFormMap.class);
		receiveFormMap.put("state", 0);
		receiveFormMap.put("orderby", "group by collarNumber");
		metalsBomFormMap.put("where", "where state = 0");
		receiveFormMap=toFormMap(receiveFormMap, pageNow, pageSize,receiveFormMap.getStr("orderby"));
		List<MetalsReceiveFormMap> receiveFormMaps = receiveMapper.findByPage(receiveFormMap);
		List<MetalsBomFormMap> metalsBomFormMaps = metalsBomMapper.findByWhere(metalsBomFormMap);
		List<UserFormMap> userFormMaps = userMapper.findByWhere(new UserFormMap());
		List<DepartmentForMap> departmentForMap=departmentMapper.findByWhere(new DepartmentForMap());
		List<RepairapplicationFromMap> repForMap=repairapplicationMapper.findByWhere(new RepairapplicationFromMap());
		
		for(int i=0;i<receiveFormMaps.size();i++) {
			for(int j=0;j<userFormMaps.size();j++) {
				if(receiveFormMaps.get(i).get("peopleInformationId").toString().equals(userFormMaps.get(j).get("id").toString())) {
					receiveFormMaps.get(i).set("peopleInformationId", userFormMaps.get(j).get("userName"));
				}	
			}
		}
		
		for(int i=0;i<receiveFormMaps.size();i++) {
			for(int j=0;j<metalsBomFormMaps.size();j++) {
				if(receiveFormMaps.get(i).get("metalsBomId").equals(metalsBomFormMaps.get(j).get("id"))) {
					receiveFormMaps.get(i).put("materialCode", metalsBomFormMaps.get(j).get("materialCode"));
					receiveFormMaps.get(i).put("name", metalsBomFormMaps.get(j).get("name"));
					receiveFormMaps.get(i).put("specifications", metalsBomFormMaps.get(j).get("specifications"));
					receiveFormMaps.get(i).put("unit", metalsBomFormMaps.get(j).get("unit"));
				}	
			}
		}
		
		for(int i=0;i<receiveFormMaps.size();i++) {
			for(int j=0;j<departmentForMap.size();j++) {
				if(receiveFormMaps.get(i).get("departmentId").equals(departmentForMap.get(j).get("id"))) {
					receiveFormMaps.get(i).set("departmentId", departmentForMap.get(j).get("name"));
				}	
			}
		}
		
		for(int i=0;i<receiveFormMaps.size();i++) {	
			for(int j=0;j<repForMap.size();j++) {	
				if(receiveFormMaps.get(i).get("repairApplicationId")!=null) {
					if(receiveFormMaps.get(i).get("repairApplicationId").equals(repForMap.get(j).get("id"))) {
						if(repForMap.get(j).get("formCategoryId").toString()!=null) {
							receiveFormMaps.get(i).set("repairApplicationId", repForMap.get(j).getInt("formCategoryId"));
							if(receiveFormMaps.get(i).get("repairApplicationId").toString().equals("1")) {
								receiveFormMaps.get(i).set("repairApplicationId", "模具档案");
							}else if(receiveFormMaps.get(i).get("repairApplicationId").toString().equals("2")) {
								receiveFormMaps.get(i).set("repairApplicationId", "设备台账");
							}	
						}			
					}
				}			
			}	
		}	
		
		pageView.setRecords(receiveFormMaps);
		return pageView;
	}
	
	@ResponseBody
	@RequestMapping("receive_add")
	@SystemLog(module="申请领用",methods="新增领用")//凡需要处理业务逻辑的.都需要记录操作日志
	public String receive_add() throws Exception {
			HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
			UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
			String[] id = request.getParameterValues("id");
			String[] departmentId = request.getParameterValues("departmentId");
			String[] collarNumber = request.getParameterValues("collarNumber");
			String[] metalsBomId = request.getParameterValues("metalsBomId");
			String[] number = request.getParameterValues("number");
			String[] price = request.getParameterValues("price");
			String[] total = request.getParameterValues("total");
			String[] purpose = request.getParameterValues("purpose");
			
			if(null != id) {
				for(int i=0;i<id.length;i++) {
					ReceiveFormMap receiveFormMap = new ReceiveFormMap();
					receiveFormMap.set("collarNumber", collarNumber[0]);
					receiveFormMap.set("departmentId", departmentId[0]);
					receiveFormMap.set("peopleInformationId", userFormMap.getInt("id"));
					receiveFormMap.set("metalsBomId", metalsBomId[i]);
					receiveFormMap.set("receiveDate", DateUtils.getStringDateTime());
					receiveFormMap.set("number", number[i]);
					receiveFormMap.set("price", price[i]);
					receiveFormMap.set("total", total[i]);
					receiveFormMap.set("purpose", purpose[i]);
					receiveFormMap.set("state", "0");
					receiveMapper.addEntity(receiveFormMap);
				}
				return "success";
			}
		return "false";
	}
	
	@RequestMapping(value="receive_statisics_details")
	public String receive_statisics_details(Model model) throws Exception {
		//获取采购单号	
		String id = getPara("id");
		ReceiveFormMap receiveFormMap = receiveMapper.findbyFrist("id", id, ReceiveFormMap.class);
		List<ReceiveFormMap> receiveFormMaps = receiveMapper.findByAttribute("collarNumber", receiveFormMap.get("collarNumber").toString(), ReceiveFormMap.class);
		List<MetalsBomFormMap> metalsBomFormMaps = new ArrayList<MetalsBomFormMap>();
		List<UserFormMap> userFormMap = userMapper.findByWhere(new UserFormMap());
		List<UserFormMap> userFormMaps = new ArrayList<UserFormMap>();
		List<DepartmentForMap> departmentForMaps=departmentMapper.findByWhere(new DepartmentForMap());
		List<AuditLoggingFormMap> auditLoggingFormMaps=auditLoggingMapper.getAuditLoggingByAuditId(id,Integer.parseInt("1"));
		PositionFormMap positionFormMap = new PositionFormMap();
		positionFormMap.put("where", "where position_name LIKE '%主管%' and departid = "+receiveFormMap.getInt("departmentId"));
		List<PositionFormMap> positionFormMaps=positionMapper.findByWhere(positionFormMap);
		
		for(int i=0;i<departmentForMaps.size();i++) {
			if(receiveFormMap.get("departmentId").toString().equals(departmentForMaps.get(i).get("id").toString())) {
				receiveFormMap.set("departmentId", departmentForMaps.get(i).get("name").toString());
			}
		}
		
		for(int i=0;i<15;i++) {
			
		}
		
		for(int i=0;i<userFormMap.size();i++) {
			if(receiveFormMap.get("peopleInformationId").toString().equals(userFormMap.get(i).get("id").toString())) {
				receiveFormMap.put("peopleInformationId", userFormMap.get(i).get("userName"));
			}
		}
		
		for(int i=0;i<auditLoggingFormMaps.size();i++) {
			for(int j=0;j<userFormMap.size();j++) {
				if(auditLoggingFormMaps.get(i).get("userId").toString().equals(userFormMap.get(j).get("id").toString())) {
					userFormMap.get(j).put("auditTime", auditLoggingFormMaps.get(i).get("auditTime"));
					userFormMaps.add(i, userFormMap.get(j));
				}
			}		
		}
		
		for(int i=0;i<receiveFormMaps.size();i++) {
				MetalsBomFormMap metalsBomFormMap = metalsBomMapper.findbyFrist("id", receiveFormMaps.get(i).get("metalsBomId").toString(), MetalsBomFormMap.class);
				receiveFormMaps.get(i).set("materialCode", metalsBomFormMap.get("materialCode").toString());
				receiveFormMaps.get(i).set("name", metalsBomFormMap.get("name").toString());
				receiveFormMaps.get(i).set("specifications", metalsBomFormMap.get("specifications").toString());
				receiveFormMaps.get(i).set("unit", metalsBomFormMap.get("unit").toString());
				if(metalsBomFormMap.get("oldForNewService").toString().equals("0")) {
					receiveFormMaps.get(i).put("oldForNewService", "否");
				}else {
					receiveFormMaps.get(i).put("oldForNewService", "是");
				}
		}
		
		model.addAttribute("receive", receiveFormMap);
		model.addAttribute("position", positionFormMaps);
		model.addAttribute("receives", receiveFormMaps);
		model.addAttribute("userFormMaps", userFormMaps);
		return Common.BACKGROUND_PATH + "/metals/receive/receive_statistics_details";										 
	}
	
	@ResponseBody
	@RequestMapping(value="receive_statisics_deleteEntity")
	@SystemLog(module="领用统计",methods="领用-删除领用单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		ReceiveFormMap receiveFormMap =  receiveMapper.findbyFrist("id", ids[0], ReceiveFormMap.class);
		List<ReceiveFormMap> receiveFormMaps = receiveMapper.findByAttribute("collarNumber", receiveFormMap.get("collarNumber").toString(), ReceiveFormMap.class);
		for(int i=0;i<receiveFormMaps.size();i++){
			receiveFormMaps.get(i).set("state", 1);
			receiveMapper.editEntity(receiveFormMaps.get(i));						
		}	
		return "success";
	}
	
	@RequestMapping("receive_acceptance")
	@ResponseBody
	@SystemLog(module="申请领用",methods="验收领用")//凡需要处理业务逻辑的.都需要记录操作日志
	public String receive_acceptance() throws Exception {
		String id = getPara("id");
		ReceiveFormMap receiveFormMap =  receiveMapper.findbyFrist("id", id, ReceiveFormMap.class);
		String collarNumber = receiveFormMap.get("collarNumber").toString();
		List<ReceiveFormMap> receiveFormMaps = receiveMapper.findByAttribute("collarNumber", collarNumber, ReceiveFormMap.class);
		
		AuditLoggingFormMap auditLoggingFormMap = receiveMapper.getAllByCollarNumber(receiveFormMap.getStr("collarNumber").toString());
		if(auditLoggingFormMap!=null) {			
			MetalsBomFormMap metalsBomFormMap = new MetalsBomFormMap();
			metalsBomFormMap.put("where", "where state=0");
			List<MetalsBomFormMap> metalsBomFormMaps = metalsBomMapper.findByWhere(new MetalsBomFormMap());
			
			for(int i=0;i<receiveFormMaps.size();i++) {
				for(int j=0;j<metalsBomFormMaps.size();j++) {
					if(receiveFormMaps.get(i).get("metalsBomId").toString().equals(metalsBomFormMaps.get(j).get("id").toString())) {
						int sum1 = metalsBomFormMaps.get(j).getInt("inventoryTotal");
						int sum2 = receiveFormMaps.get(i).getInt("number");
						int inventoryTotal = sum1-sum2;				
						metalsBomFormMaps.get(j).set("inventoryTotal",inventoryTotal);
						metalsBomMapper.editEntity(metalsBomFormMaps.get(j));
						
						PurchaseSesaleStockFromMap sesaleStock = new PurchaseSesaleStockFromMap();
						sesaleStock.set("materialId", metalsBomFormMaps.get(j).get("id"));
						sesaleStock.set("receiveId", receiveFormMap.get("id"));
						sesaleStock.set("date", DateUtils.getStringDateTime());
						sesaleStock.set("currentOutNumber", receiveFormMaps.get(i).get("number"));
						sesaleStock.set("currentOutMoney", receiveFormMaps.get(i).get("total"));
						sesaleStock.set("state", "0");
						purchasesalestockMapper.addEntity(sesaleStock);
					}
						
				}
							
			}
			for (ReceiveFormMap rec : receiveFormMaps) {
				rec.set("state", "2");
				receiveMapper.editEntity(rec);
				
			}
			return "success";
		}
		return "false";
	}
}
